package felix.things.router.mqtt.server.store.utils;

import cn.hutool.core.util.StrUtil;

import java.util.List;

/**
 * @author Felix
 * @date 2019/8/21
 **/
public class TopicUtils {
    public static String topicConvert(String topic, String topicFilter) {
        List<String> splitTopics = StrUtil.split(topic, '/');
        List<String> spliteTopicFilters = StrUtil.split(topicFilter, '/');
        StringBuilder newTopicFilter = new StringBuilder();
        for (int i = 0; i < spliteTopicFilters.size(); i++) {
            String value = spliteTopicFilters.get(i);
            if ("+".equals(value)) {
                newTopicFilter.append("+/");
            } else if ("#".equals(value)) {
                newTopicFilter.append("#/");
                break;
            } else {
                newTopicFilter.append(splitTopics.get(i)).append("/");
            }
        }
        newTopicFilter = new StringBuilder(StrUtil.removeSuffix(newTopicFilter.toString(), "/"));
        return newTopicFilter.toString();
    }
}
